package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcLevelCareMapper;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.domain.OmcLevelCare;
import com.csun.cmny.provider.model.dto.levelCare.LevelCareSelectDto;
import com.csun.cmny.provider.model.dto.levelCare.OmcLevelCareSaveDto;
import com.csun.cmny.provider.model.dto.levelCare.OmcLevelCareUpdateDto;
import com.csun.cmny.provider.model.vo.levelCare.LevelCareVo;
import com.csun.cmny.provider.service.OmcLevelCareService;
import com.csun.cmny.util.PublicUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
@Transactional
public class OmcLevelCareServiceImpl extends BaseService<OmcLevelCare> implements OmcLevelCareService {

    @Resource
    private OmcLevelCareMapper levelCareMapper;

    @Override
    public int addLevelCare(OmcLevelCareSaveDto levelCareSaveDto, LoginAuthDto loginAuthDto) {

        OmcLevelCare omcLevelCare = new OmcLevelCare();
        //判断此机构是否有同名的护理等级
        omcLevelCare.setNursingCare(levelCareSaveDto.getNursingCare());
        omcLevelCare.setGroupId(levelCareSaveDto.getGroupId());


        List<OmcLevelCare> select = select(omcLevelCare);

        if (PublicUtil.isNotEmpty(select)){
            throw new OmcBizException(ErrorCodeEnum.OMC10080045);
        }

        BeanUtils.copyProperties(levelCareSaveDto,omcLevelCare);
        omcLevelCare.setOperator(loginAuthDto.getUserName());
        omcLevelCare.setUserId(loginAuthDto.getUserId());
        return save(omcLevelCare);
    }

    @Override
    public int updateLevelCare(OmcLevelCareUpdateDto levelCareUpdateDto, LoginAuthDto loginAuthDto) {

        OmcLevelCare omcLevelCare = new OmcLevelCare();
        //判断此机构是否有同名的护理等级
        omcLevelCare.setNursingCare(levelCareUpdateDto.getNursingCare());
        omcLevelCare.setGroupId(levelCareUpdateDto.getGroupId());

        List<OmcLevelCare> select = select(omcLevelCare);

        for (OmcLevelCare levelCare : select) {
            if (!levelCare.getId().equals(levelCareUpdateDto.getId())){
                throw new OmcBizException(ErrorCodeEnum.OMC10080045);
            }
        }

        BeanUtils.copyProperties(levelCareUpdateDto,omcLevelCare);
        return update(omcLevelCare);
    }

    @Override
    public List<LevelCareVo> selectLevelCare(LevelCareSelectDto levelCareSelectDto) {
        return levelCareMapper.selectLevelCare(levelCareSelectDto);
    }
}
